/**
 * 
 */
package niuke;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 * @author zhongfang
 *
 */
public class 样本挖掘 {
//	 10
//	 6 1 2 3 4 5 6
//	 2 4 10
//	 3 1 2 3
//	 4 1 2 2 3
//	 4 6 3 3 9
//	 4 7 8 2 5
//	 4 5 7 8 3
//	 4 7 6 5 9
//	 5 1 5 8 7 0
//	 5 10 9 8 4 3
//	 3
//	 0
//	 3 1 2 3
//	 3 10 4 10
	// boolean tag=true;
	// for (int ikkk = 1; ikkk < test.length; ikkk++) {
	// if (test[ikkk] != sample[ikkk]) {
	// tag=false;
	// break;
	// }
	// }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		int[][] samples = new int[N][];
		for (int i = 0; i < N; i++) {
			int m = scanner.nextInt();
			samples[i]=new int[m+1];
			for (int j = 1; j <= m; j++) {
				samples[i][j] = scanner.nextInt();
			}
		}
		int k = scanner.nextInt();
		for (int i = 0; i < k; i++) {
			int m = scanner.nextInt();
			int[] test = new int[m + 1];
			for (int j = 1; j <= m; j++) {
				test[j] = scanner.nextInt();
			}
			m++;
//			display(test);
			StringBuilder reStringBuilder = new StringBuilder();
			ArrayList<Integer> res = new ArrayList<>();
			for (int j = 0; j < N; j++) {
				boolean tag = false;
				if (m == samples[j].length) {
					tag = same(samples[j], test);
				}else if (m+1==samples[j].length) {
					tag=similar(test, samples[j]);
				}else if (m-1==samples[j].length) {
					tag=similar(samples[j], test);
				}
//				display(samples[j]);
//				System.out.println(tag);
				if(tag){
					res.add(j+1);
				}
			}
			Collections.sort(res);
			reStringBuilder.append(res.size() + " ");
			for (int j2 = 0; j2 < res.size(); j2++) {//
				reStringBuilder.append(res.get(j2) + " ");
			}
			System.out.println(reStringBuilder.substring(0, reStringBuilder.length() - 1));
		}
	}

	private static void display(int[] is) {
		for (int i : is) {
			System.out.print(i+" ");
		}
		System.out.println();
	}

	private static boolean same(int[] is, int[] test) {
		for (int ikkk = 0; ikkk < test.length; ikkk++) {
			if (test[ikkk] != is[ikkk]) {
				return false;
			}
		}
		return true;
	}

	private static boolean similar(int[] test, int[] is) {// 前短后长
		boolean lastChance = true;
		for (int i = 0, j = 0; i < test.length; i++, j++) {
			if (test[i] != is[j]) {
				if (!lastChance) {
					return false;
				} else {
					j++;
					lastChance = false;
				}
			}
		}
		return true;
	}

}
